#pragma once
#include <iostream>
// #include <vector>
// #include <optional>
#include <filesystem>
#include <string>
#include <fstream>
#include <sstream>
// #include <map>
#include <curl/curl.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/gzip_stream.h>
#include "svga.pb.h"

size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s) {
	size_t newLength = size * nmemb;
	try {
		s->append((char*)contents, newLength);
	}
	catch (std::bad_alloc& e) {
		std::cerr << "Exception caught: " << e.what() << std::endl;
		// handle memory alloc failure
		return 0;
	}
	return newLength;
}
size_t WriteCallbackWrapper(void* contents, size_t size, size_t nmemb, void* userp) {
	return WriteCallback(contents, size, nmemb, static_cast<std::string*>(userp));
}
void LoadFromMemory(const char* _bytes, size_t _size) {
	if (_size < 4) {
		return;
	}
	// version 2
	std::stringstream _ss;
	_ss.write(_bytes, (std::streamsize)_size);
	google::protobuf::io::IstreamInputStream _iis(&_ss);
	google::protobuf::io::GzipInputStream _gis(&_iis);
	com::opensource::svga::MovieEntity _me;
	if (_me.ParseFromZeroCopyStream(&_gis)) {
		float _width = _me.params().viewboxwidth();
		float _height = _me.params().viewboxheight();
		std::cout << "width: " << _width << ", height: " << _height << std::endl;
		//auto render = std::make_unique<Render>(_me, _width, _height);
		//result = render->isSuccess;
	}
}
void LoadFromHttp(const char* _path) {
	try {
		CURLcode res;
		std::string strResponse;
		curl_global_init(CURL_GLOBAL_DEFAULT);
		CURL* curl = curl_easy_init();
		if (nullptr == curl)
		{
			std::cout << "https init fail!" << std::endl;
			return;
		}

		curl_easy_setopt(curl, CURLOPT_URL, _path);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallbackWrapper);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strResponse);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);//设定为不验证证书和HOST
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		curl_global_cleanup();
		if (res != CURLE_OK) {
			std::cerr << "https request failed: " << curl_easy_strerror(res) << std::endl;
			return;
		}
		return LoadFromMemory(strResponse.c_str(), strResponse.size());
	}
	catch (const std::exception& e) {
		std::cerr << "Exception caught: " << e.what() << std::endl;
	}
}
void LoadFromFile(const char* _path) {
	std::ifstream _ifs(_path, std::ios::binary);
	_ifs.seekg(0, std::ios::end);
	size_t _fsz = (size_t)_ifs.tellg();
	_ifs.seekg(0, std::ios::beg);
	std::vector<char> _bytes;
	_bytes.resize(_fsz);
	_ifs.read(&_bytes[0], (std::streamsize)_fsz);
	_ifs.close();

	LoadFromMemory(&_bytes[0], _bytes.size());
}

int main(int argc, char* argv[]) {
    std::cout << "Protocol Buffers version: " << google::protobuf::internal::VersionString(GOOGLE_PROTOBUF_VERSION) << std::endl;
    //const char* _path = "E:\\cpp-learn\\test.svga";"E:\\GSvga\\input\\tkm.svga";
	const char* _path = "https://oss.cdddian.com/im/2023-12-01/765f196f123c40d28c58126a0984fea7391232.svga";
    // LoadFromFile(_path);
	LoadFromHttp(_path);
    /*auto ret = std::make_unique<Parse>(_path);//.c_str()
    if (ret->result) {
        std::cout << "convert success" << std::endl;
    }*/
    std::cin.get();
    return 0;
}